package com.example.config;

import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.core.config.GlobalConfig;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.apache.ibatis.plugin.Interceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

@Configuration
public class MybatisPlusConfig {

//    /**
//     * 打印 sql
//     */
//    @Bean
//    public PerformanceInterceptor performanceInterceptor() {
//        PerformanceInterceptor performanceInterceptor = new PerformanceInterceptor();
//        //格式化sql语句
//        Properties properties = new Properties();
//        properties.setProperty("format", "true");
//        performanceInterceptor.setProperties(properties);
//        return performanceInterceptor;
//    }


//    @Bean
//    public MybatisSqlSessionFactoryBean getSessionFactoryBean() {
//        MybatisSqlSessionFactoryBean sqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();
//        // 源码里面如果有configuration, 不会注入BaseMapper里面的方法, 所以这里要这样写
//        List<Interceptor> interceptors = new ArrayList<>();
//        // 如果是dev环境,打印出sql, 设置sql拦截插件, prod环境不要使用, 会影响性能
//        PerformanceInterceptor performanceInterceptor = new PerformanceInterceptor();
//        interceptors.add(performanceInterceptor);
//        sqlSessionFactoryBean.setPlugins(interceptors.toArray(new Interceptor[0]));
//        return sqlSessionFactoryBean;
//    }


    /**
     * mybatis-plus分页插件
     */
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        PaginationInterceptor page = new PaginationInterceptor();
        page.setDialectType("mysql");
        return page;
    }
}
